package com.qiangqiang.task.server.controller;

import com.qiangqiang.task.server.entity.ReceviedTask;
import com.qiangqiang.task.server.entity.Task;
import com.qiangqiang.task.server.form.ReceivedTaskForm;
import com.qiangqiang.task.server.service.TaskService;
import com.qiangqiang.task.server.util.ResultVOUtil;
import com.qiangqiang.task.server.vo.ResultVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping(value = "/task")
public class TaskController {

    @Autowired
    private TaskService taskService;

    @GetMapping("/listByUserId")
    public ResultVO findAllByUserId(@Param("page") int page,
                                    @Param("pageSize") int pageSize,
                                    @Param("userId") long userId,
                                    @Param("isReceviedId") boolean isReceviedId) {
        return ResultVOUtil.success(taskService.findAllByUserId(page, pageSize, userId,isReceviedId));
    }

    /**
     * @Description: 领取任务
     * @Param:
     * @return:
     * @Author: sky.lin
     * @Date: 2018/12/15
     */
    @PostMapping("/receivedTask")
    public ResultVO receivedTask(ReceivedTaskForm receivedTaskForm){
        taskService.receiveTask(receivedTaskForm.getReceiverId(),receivedTaskForm.getTaskId());
        return ResultVOUtil.success();
    }

    /**
     * @param [taskId]
     * @return com.qiangqiang.task.server.vo.ResultVO
     * @author sky
     * @date 2018/7/23
     * @describe: 仅被内部调用
     */
    @RequestMapping(value = "/getStatusByTaskId", method = RequestMethod.GET)
    public Integer showStatusByTaskId(@Param("taskId") long taskId) {
        return taskService.findStatusByTaskId(taskId);
    }


    @PostMapping("/addTask")
    public ResultVO addTask(Task task){
        return ResultVOUtil.success(taskService.addTask(task));
    }

    @GetMapping(value = "/findUnreceivedTask")
    public ResultVO findUnreceivedTask(@Param("page") int page,
                                       @Param("pageSize") int pageSize){
        return ResultVOUtil.success(taskService.findAllByStatus(page,pageSize));
    }
    @PostMapping("/finishTask")
    public ResultVO finishTask(@Param("taskId") long taskId){
        return ResultVOUtil.success(taskService.finishTask(taskId));
    }
}
